﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="AddShop.aspx.cs" Inherits="WebUI_Shop_AddShop" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
    <div>
        店铺类型：
        <asp:DropDownList ID="ddlShopCategories" onchange="mainCategoryIndexChanged(this);"
            runat="server">
        </asp:DropDownList>
    </div>
    <div>
        店铺名称：<input id="txtShopName" type="text" />
    </div>
    <div>
        地址：<input id="txtAddr" type="text" />
    </div>
    <div>
        电话：<input id="txtTel" type="text" />
    </div>
    <div>
        交通方式：<input id="txtBusLine" type="text" />
    </div>
    <div>
        营业时间：<input id="txtServiceHours" type="text" />
    </div>
    <div>
        店铺邮编：<input id="txtPostCode" type="text" />
    </div>
    <div>
        店铺介绍：<input id="txtShopDesc" type="text" />
    </div>
    <div>
        商铺特色描述：<input id="txtSpecialDesc" type="text" />
    </div>
    <hr />
    <div>
        扩展属性：</div>
    <div id="divExtensiveProp">
    </div>
    <div>
        <input type="button" id="btnSave" />
    </div>
    <script type="text/javascript">

        $().ready(function () {
            $("#Form1").validate({
                rules: {
                    MainContent_ddlShopCategories:{
                        range:[0,10000]
                    },
                    txtShopName: {
                        required: true
                    },
                    txtAddr: {
                        required: true
                    },
                    txtTel: {
                        required: true
                    },
                    txtBusLine: {
                        required: true
                    },
                    txtServiceHours: {
                        required: true,
                    },
                    txtPostCode: {
                        required: true,
                        digits: true
                    },
                    txtShopDesc: {
                        required: true
                    },
                    txtSpecialDesc: {
                        required: true
                    }
                },
                messages: {
                    MainContent_ddlShopCategories: {
                        required: "请选择店铺类型！"
                    },
                    txtShopName: {
                        required: "请输入店铺名称！"
                    },
                    txtAddr: {
                        required: "请输入店铺地址！"
                    },
                    txtTel: {
                        required: "请输入店铺号码！"
                    },
                    txtBusLine: {
                        required: "请输入交通方式！"
                    },
                    txtServiceHours: {
                        required: "请输入营业时间！"
                    },
                    txtPostCode: {
                        required: "请输入邮政编码！"
                    },
                    txtShopDesc: {
                        required: "请选择店铺类型！"
                    },
                    txtSpecialDesc: {
                        required: "请选择店铺类型！"
                    },
                }
            });
        });

        function mainCategoryIndexChanged(ctrl) {
            //清除后面的元素
            $(ctrl).nextAll().remove();

            var categroryId = parseInt($(ctrl).val());
            if (categroryId < 0) {
                //生成扩展属性
                refreshExtensiveProp(categroryId);
                return;
            }
            var res = ASP.webui_shop_addshop_aspx.GetChildCategory(categroryId);
            if (res.value && res.value.length > 0) {
                //具有子分裂分类
                appendChildCategory(res, $(ctrl));
            }
            else {
                //生成扩展属性
                refreshExtensiveProp(categroryId);
            }
        }

        function appendChildCategory(ajaxRes, prevCtrl) {
            if (ajaxRes.value) {
                var sb = new StringBuilder();
                sb.appendFormat("<option value={0}>{1}</option>", -1, "--请选择--");
                for (var i = 0; i < ajaxRes.value.length; i++) {
                    sb.appendFormat("<option value={0}>{1}</option>", ajaxRes.value[i].Id, ajaxRes.value[i].CategoryName);
                }

                var ctrl = $("<select>").html(sb.toString()).appendTo(prevCtrl.parent());
                ctrl.onchange = function () { mainCategoryIndexChanged(ctrl); }
            }
            else {
                local.mesagebox("请求失败！");
            }
        }

        function refreshExtensiveProp(categoryId) {
            var extPropCtrl = $("#divExtensiveProp");

            if (categoryId < 0) {
                //无扩展属性
                extPropCtrl.html("");
                return;
            }

            var res = ASP.webui_shop_addshop_aspx.GetExtensiveProperties(categoryId);
            if (res.value && res.value.length > 0) {
                var sb = new StringBuilder();
                for (var i = 0; i < res.value.length; i++) {
                    sb.appendFormat('<div> {0} <input type="text" name="ext_prop_{1}_{2}" /></div>',
                    res.value[i].PropName, res.value[i].Id, i);
                }

                extPropCtrl.html(sb.toString());
            }
            else {
                //无扩展属性
                extPropCtrl.html("");
            }
        }

    </script>
</asp:Content>
